import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Req,
  Res,
  UploadedFile,
  UseInterceptors,
} from '@nestjs/common';
import { GenerativeAiAchievementService } from './generative-ai-achievement.service';
import { FileInterceptor } from '@nestjs/platform-express';
import { CreateGenerativeAIAchievementDto } from './dto/create-generative-ai-achievement.dto';
import { Request } from 'express';
import { request, type Response } from 'express';
import { Public } from 'src/common/decorator/public.decorator';

@Controller('generative-ai-achievement')
export class GenerativeAiAchievementController {
  constructor(
    private readonly generativeAiAchievementService: GenerativeAiAchievementService,
  ) {}

  @Post('uploadAchievementItem')
  @UseInterceptors(FileInterceptor('file'))
  uploadAchievementItem(
    @Body() createGenerativeAIAchievementDto: CreateGenerativeAIAchievementDto,
    @UploadedFile() file,
    @Req() request: Request,
  ) {
    const user = request.user;
    createGenerativeAIAchievementDto.uploader = (user as any).email;
    return this.generativeAiAchievementService.uploadAchievementItem(
      createGenerativeAIAchievementDto,
      file,
    );
  }

  @Get('getAllAchievement')
  getAllAchievement() {
    return this.generativeAiAchievementService.getAllAchievement();
  }

  @Get('getAchievementById/:id')
  getAllAchievementById(@Param('id') id: string) {
    return this.generativeAiAchievementService.getAchievementById(id);
  }

  @Get('getAchievementByType/:type')
  getAchievementByType(@Param('type') type: string) {
    return this.generativeAiAchievementService.getAchievementByType(type);
  }

  @Delete('deleteAchievementById/:id')
  deleteAchievementById(@Param('id') id: string) {
    return this.generativeAiAchievementService.deleteAchievementById(id);
  }

  @Public()
  @Get('getFileItemContent/:id')
  getFileItemContent(@Param('id') id: string, @Res() response: Response) {
    return this.generativeAiAchievementService.getFileItemContent(id, response);
  }

  @Public()
  @Get('downloadAIProductionItemStream/:id')
  downloadAIProductionItemStream(
    @Param('id') id: string,
    @Res() response: Response,
  ) {
    return this.generativeAiAchievementService.downloadAIProductionItemStream(
      id,
      response,
    );
  }
}
